/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
/**
 * Created by Z on 2019/8/26.
 */

import Vue from 'vue';

/* 获取Promise的方式的action*/

let successCode = 200;
export function getPromiseAction (p, commit, mutationType) {
    let that = Vue.prototype;
    let promise = new Promise((resolve, reject) => {
        p.then(res => {
            if (res.code === successCode) {
                commit(mutationType, res.data);
                return resolve(res);
            }
            return that.$showErr(res.message);
            reject();
        }, error => {
            return that.$showErr(error);
        }).catch(error => {
            return that.$showErr(error);
        });
    });
    return promise;
};


/* 获取Promise的方式的action，无mutations*/
export function getPromiseActionNoMutations (p) {
    let that = Vue.prototype;
    let promise = new Promise((resolve, reject) => {
        p.then(res => {
            if (res.code === successCode) {
                return resolve(res);
            }
            return that.$showErr(res.message);
            reject();
        }, error => {
            return that.$showErr(error);
        }).catch(error => {
            return that.$showErr(error);
        });
    });
    return promise;
}
